package light.head.dao.oa;

import java.util.Date;
import java.util.List;

import light.head.framework.base.BaseDAO;
import light.head.model.ConsumingItem;

import org.springframework.stereotype.Service;
/***
 * 物品领用明细
 * @author yy
 *
 */
@Service
public class ConsumingItemDAO extends BaseDAO<ConsumingItem, Integer>{
	
	private final static String sql_findByGC = " FROM ConsumingItem item WHERE item.gc = ? AND (SELECT con.cc AND con.ct FROM Consuming con WHERE con.code = item.coc);";
	private static final String hql_findByConsumingCode = " FROM ConsumingItem ci WHERE (ci.deleted = 0 OR ci.deleted IS NULL) AND ci.coc = ? ORDER BY ci.code";
	
	
	
	public List<ConsumingItem> findByConCode(Integer cdCode) {
		if (cdCode != null) return find(hql_findByConsumingCode, cdCode);
		return null;
	}
	
	
	public List<ConsumingItem> findByConCodes(List<Object> cdCodes) {
		if (null == cdCodes || cdCodes.size() == 0) return null;
		
		StringBuffer hql = new StringBuffer(" FROM ConsumingItem ci WHERE (ci.deleted = 0 OR ci.deleted IS NULL) AND ci.coc IN (");
		for (int i = 0; i < cdCodes.size(); i++) {
			hql.append("?,");
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), null, null, cdCodes.toArray());
	}
	
	
	
	/**
	 * 根据机构编号oc 和 商品编号gc 获得商品领用明细
	 */
	public List<ConsumingItem> findByGc(Integer gc){
		String hql = " FROM ConsumingItem ci WHERE ci.gc = ?";
		return findI(hql,gc);
	}
	
	
	/**
	 * 根据商品编号获取当前机构的库存情况
	 */
	public List<String> findByGC(Integer cc, Integer gc, Date ct){
		if(cc == null || null == cc) return null;
		return find(sql_findByGC, null, null, cc, gc, ct);
	}
}
